package net.sk.china.city.service.impl;

import net.sk.china.city.domain.ArTeam;
import net.sk.china.city.domain.SysDept;
import net.sk.china.city.domain.dto.team.TeamSearchDTO;
import net.sk.china.city.domain.dto.team.TeamUpdateDTO;
import net.sk.china.city.domain.dto.user.TeamPairDTO;
import net.sk.china.city.mapper.ArTeamMapper;
import net.sk.china.city.mapper.SysDeptMapper;
import net.sk.china.city.service.IArTeamService;
import net.sk.china.city.utils.PlatformIdHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 志愿者队伍Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-04-07
 */
@Service
public class ArTeamServiceImpl implements IArTeamService
{
    @Autowired
    private ArTeamMapper arTeamMapper;
    @Autowired
    private SysDeptMapper sysDeptMapper;

    /**
     * 查询志愿者队伍
     * 
     * @param pkId 志愿者队伍主键
     * @return 志愿者队伍
     */
    @Override
    public ArTeam selectArTeamByPkId(Long pkId)
    {
        return arTeamMapper.selectArTeamByPkId(pkId);
    }

    /**
     * 查询志愿者队伍列表
     * 
     * @param teamSearchDTO 志愿者队伍
     * @return 志愿者队伍
     */
    @Override
    public List<ArTeam> selectArTeamList(TeamSearchDTO teamSearchDTO)
    {
        String platformId = PlatformIdHolder.getPlatformId();
        teamSearchDTO.setPlatformId(platformId);
        return arTeamMapper.selectArTeamList(teamSearchDTO);
    }

    /**
     * 新增志愿者队伍
     *
     * @param teamUpdateDTO 志愿者队伍
     * @return 结果
     */
    @Override
    public int insertArTeam(TeamUpdateDTO teamUpdateDTO)
    {
        SysDept sysDept = sysDeptMapper.selectDeptById(teamUpdateDTO.getSiteId());
        String ancestors = sysDept.getAncestors();
        Long deptId = sysDept.getDeptId();
        String platformId = ancestors + ',' + deptId;
        teamUpdateDTO.setPlatformId(platformId);
        teamUpdateDTO.setCreateDatetime(LocalDateTime.now());
        teamUpdateDTO.setUpdateDatetime(LocalDateTime.now());
        return arTeamMapper.insertArTeam(teamUpdateDTO);
    }

    /**
     * 修改志愿者队伍
     *
     * @param teamUpdateDTO 志愿者队伍
     * @return 结果
     */
    @Override
    public int updateArTeam(TeamUpdateDTO teamUpdateDTO)
    {
        SysDept sysDept = sysDeptMapper.selectDeptById(teamUpdateDTO.getSiteId());
        String ancestors = sysDept.getAncestors();
        Long deptId = sysDept.getDeptId();
        String platformId = ancestors + ',' + deptId;
        teamUpdateDTO.setPlatformId(platformId);
        teamUpdateDTO.setUpdateDatetime(LocalDateTime.now());
        return arTeamMapper.updateArTeam(teamUpdateDTO);
    }

    /**
     * 批量删除志愿者队伍
     * 
     * @param pkIds 需要删除的志愿者队伍主键
     * @return 结果
     */
    @Override
    public int deleteArTeamByPkIds(Long[] pkIds)
    {
        return arTeamMapper.deleteArTeamByPkIds(pkIds);
    }

    /**
     * 删除志愿者队伍信息
     * 
     * @param pkId 志愿者队伍主键
     * @return 结果
     */
    @Override
    public int deleteArTeamByPkId(Long pkId)
    {
        return arTeamMapper.deleteArTeamByPkId(pkId);
    }

    @Override
    public List<TeamPairDTO> selectTeamOptionList() {
        TeamSearchDTO teamSearch = new TeamSearchDTO();
        String platformId = PlatformIdHolder.getPlatformId();
        teamSearch.setPlatformId(platformId);
        List<ArTeam> arTeams = arTeamMapper.selectArTeamList(teamSearch);
        return arTeams.stream().map(arTeam -> {
            TeamPairDTO teamPairDTO = new TeamPairDTO();
            teamPairDTO.setId(arTeam.getPkId());
            teamPairDTO.setName(arTeam.getTeamName());
            return teamPairDTO;
        }).collect(Collectors.toList());
    }

    @Override
    public ArTeam getTeamByTeamName(String teamName) {
        return arTeamMapper.selectArTeamByTeamName(teamName);
    }

    @Override
    public List<ArTeam> selectAllTeam() {
        return arTeamMapper.selectAllTeam();
    }
}
